package com.dubbo.framework.protocol.http;

import cn.hutool.core.convert.Convert;
import cn.hutool.setting.dialect.Props;
import com.dubbo.framework.URL;
import com.dubbo.framework.config.ServerConfig;
import org.apache.catalina.*;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.core.StandardEngine;
import org.apache.catalina.core.StandardHost;
import org.apache.catalina.startup.Tomcat;

/**
 * @author by Mr. Li 2021/4/28 18:00
 */
public class HttpServer {

    public void start() {

        final Props props = new Props("application.properties");
        final URL url = new URL(Convert.toStr(props.get("ip.addr")), Convert.toInt(props.get("ip.port")));

        //httpServer.start(IPADDR, PORT);

        // 启动http 服务器 -> tomcat/jetty
        // 读取配置

        Tomcat tomcat = new Tomcat();

        Server server = tomcat.getServer();
        // 一定是Tomcat  任何 大小写不一致 都会导致 获取不到容器
        final Service service = server.findService("Tomcat");

        Connector connector = new Connector();
        connector.setPort(url.getPort());

        Engine engine = new StandardEngine();
        engine.setDefaultHost(url.getIpAddr());

        Host host = new StandardHost();
        host.setName(url.getIpAddr());

        String contextPath = "";
        Context context = new StandardContext();
        context.setPath(contextPath);
        context.addLifecycleListener(new Tomcat.FixContextListener());

        host.addChild(context);
        engine.addChild(host);

        service.setContainer(engine);
        service.addConnector(connector);

        // 将接受到的所有请求都交给DispatcherServlet处理
        tomcat.addServlet(contextPath, "dispatcher", new DispatcherServlet());
        context.addServletMappingDecoded("/*", "dispatcher");

        try {
            // 启动
            tomcat.start();
            // 想服务信息注册到 zk中
//            ServerConfig.up(url);

            tomcat.getServer().await();
        } catch (LifecycleException e) {
            e.printStackTrace();
        }
    }

    public void start(String ipAddr, int port) {
        // 启动http 服务器 -> tomcat/jetty
        // 读取配置

        Tomcat tomcat = new Tomcat();

        Server server = tomcat.getServer();
        // 一定是Tomcat  任何 大小写不一致 都会导致 获取不到容器
        final Service service = server.findService("Tomcat");

        Connector connector = new Connector();
        connector.setPort(port);

        Engine engine = new StandardEngine();
        engine.setDefaultHost(ipAddr);

        Host host = new StandardHost();
        host.setName(ipAddr);

        String contextPath = "";
        Context context = new StandardContext();
        context.setPath(contextPath);
        context.addLifecycleListener(new Tomcat.FixContextListener());

        host.addChild(context);
        engine.addChild(host);

        service.setContainer(engine);
        service.addConnector(connector);

        // 将接受到的所有请求都交给DispatcherServlet处理
        tomcat.addServlet(contextPath, "dispatcher", new DispatcherServlet());
        context.addServletMappingDecoded("/*", "dispatcher");

        try {
            // 启动
            tomcat.start();

            // 想服务信息注册到 zk中
//            ServerConfig.up(new URL(ipAddr, port));

            tomcat.getServer().await();
        } catch (LifecycleException e) {
            e.printStackTrace();
        }
    }
}
